use std::sync::Arc;

/// These tests are common between all libraries doing local evaluation of feature flags.
/// This ensures there are no mismatches between implementations.
use feature_flags::{
    cohorts::cohort_cache_manager::CohortCacheManager,
    flags::{
        flag_match_reason::FeatureFlagMatchReason,
        flag_matching::{FeatureFlagMatch, FeatureFlagMatcher},
    },
    utils::test_utils::{create_flag_from_json, setup_pg_reader_client, setup_pg_writer_client},
};
use serde_json::json;

#[tokio::test]
async fn it_is_consistent_with_rollout_calculation_for_simple_flags() {
    let flags = create_flag_from_json(Some(
        json!([{
            "id": 1,
            "key": "simple-flag",
            "name": "Simple flag",
            "active": true,
            "deleted": false,
            "team_id": 1,
            "filters": {
                "groups": [
                    {
                        "properties": [],
                        "rollout_percentage": 45,
                    },
                ],
            },
        }])
        .to_string(),
    ));

    let results = vec![
        false, true, true, false, true, false, false, true, false, true, false, true, true, false,
        true, false, false, false, true, true, false, true, false, false, true, false, true, true,
        false, false, false, true, true, true, true, false, false, false, false, false, false,
        true, true, false, true, true, false, false, false, true, true, false, false, false, false,
        true, false, true, false, true, false, true, true, false, true, false, true, false, true,
        true, false, false, true, false, false, true, false, true, false, false, true, false,
        false, false, true, true, false, true, true, false, true, true, true, true, true, false,
        true, true, false, false, true, true, true, true, false, false, true, false, true, true,
        true, false, false, false, false, false, true, false, false, true, true, true, false,
        false, true, false, true, false, false, true, false, false, false, false, false, false,
        false, false, true, true, false, false, true, false, false, true, true, false, false, true,
        false, true, false, true, true, true, false, false, false, true, false, false, false,
        false, true, true, false, true, true, false, true, false, true, true, false, true, false,
        true, true, true, false, true, false, false, true, true, false, true, false, true, true,
        false, false, true, true, true, true, false, true, true, false, false, true, false, true,
        false, false, true, true, false, true, false, true, false, false, false, false, false,
        false, false, true, false, true, true, false, false, true, false, true, false, false,
        false, true, false, true, false, false, false, true, false, false, true, false, true, true,
        false, false, false, false, true, false, false, false, false, false, false, false, false,
        false, false, false, false, false, true, true, false, true, false, true, true, false, true,
        false, true, false, false, false, true, true, true, true, false, false, false, false,
        false, true, true, true, false, false, true, true, false, false, false, false, false, true,
        false, true, true, true, true, false, true, true, true, false, false, true, false, true,
        false, false, true, true, true, false, true, false, false, false, true, true, false, true,
        false, true, false, true, true, true, true, true, false, false, true, false, true, false,
        true, true, true, false, true, false, true, true, false, true, true, true, true, true,
        false, false, false, false, false, true, false, true, false, false, true, true, false,
        false, false, true, false, true, true, true, true, false, false, false, false, true, true,
        false, false, true, true, false, true, true, true, true, false, true, true, true, false,
        false, true, true, false, false, true, false, false, true, false, false, false, false,
        false, false, false, false, false, false, true, true, false, false, true, false, false,
        true, false, true, false, false, true, false, false, false, false, false, false, true,
        false, false, false, false, false, false, false, false, false, true, true, true, false,
        false, false, true, false, true, false, false, false, true, false, false, false, false,
        false, false, false, true, false, false, false, false, false, false, false, false, true,
        false, true, false, true, true, true, false, false, false, true, true, true, false, true,
        false, true, true, false, false, false, true, false, false, false, false, true, false,
        true, false, true, true, false, true, false, false, false, true, false, false, true, true,
        false, true, false, false, false, false, false, false, true, true, false, false, true,
        false, false, true, true, true, false, false, false, true, false, false, false, false,
        true, false, true, false, false, false, true, false, true, true, false, true, false, true,
        false, true, false, false, true, false, false, true, false, true, false, true, false, true,
        false, false, true, true, true, true, false, true, false, false, false, false, false, true,
        false, false, true, false, false, true, true, false, false, false, false, true, true, true,
        false, false, true, false, false, true, true, true, true, false, false, false, true, false,
        false, false, true, false, false, true, true, true, true, false, false, true, true, false,
        true, false, true, false, false, true, true, false, true, true, true, true, false, false,
        true, false, false, true, true, false, true, false, true, false, false, true, false, false,
        false, false, true, true, true, false, true, false, false, true, false, false, true, false,
        false, false, false, true, false, true, false, true, true, false, false, true, false, true,
        true, true, false, false, false, false, true, true, false, true, false, false, false, true,
        false, false, false, false, true, true, true, false, false, false, true, true, true, true,
        false, true, true, false, true, true, true, false, true, false, false, true, false, true,
        true, true, true, false, true, false, true, false, true, false, false, true, true, false,
        false, true, false, true, false, false, false, false, true, false, true, false, false,
        false, true, true, true, false, false, false, true, false, true, true, false, false, false,
        false, false, true, false, true, false, false, true, true, false, true, true, true, true,
        false, false, true, false, false, true, false, true, false, true, true, false, false,
        false, true, false, true, true, false, false, false, true, false, true, false, true, true,
        false, true, false, false, true, false, false, false, true, true, true, false, false,
        false, false, false, true, false, false, true, true, true, true, true, false, false, false,
        false, false, false, false, false, true, true, true, false, false, true, true, false, true,
        true, false, true, false, true, false, false, false, true, false, false, true, false,
        false, true, true, true, true, false, false, true, false, true, true, false, false, true,
        false, false, true, true, false, true, false, false, true, true, true, false, false, false,
        false, false, true, false, true, false, false, false, false, false, true, true, false,
        true, true, true, false, false, false, false, true, true, true, true, false, true, true,
        false, true, false, true, false, true, false, false, false, false, true, true, true, true,
        false, false, true, false, true, true, false, false, false, false, false, false, true,
        false, true, false, true, true, false, false, true, true, true, true, false, false, true,
        false, true, true, false, false, true, true, true, false, true, false, false, true, true,
        false, false, false, true, false, false, true, false, false, false, true, true, true, true,
        false, true, false, true, false, true, false, true, false, false, true, false, false, true,
        false, true, true,
    ];

    for (i, result) in results.iter().enumerate().take(1000) {
        let reader = setup_pg_reader_client(None).await;
        let writer = setup_pg_writer_client(None).await;
        let cohort_cache = Arc::new(CohortCacheManager::new(reader.clone(), None, None));

        let distinct_id = format!("distinct_id_{i}");

        let feature_flag_match = {
            let router = feature_flags::database::PostgresRouter::new(
                reader.clone(),
                writer.clone(),
                reader.clone(),
                writer.clone(),
            );
            FeatureFlagMatcher::new(distinct_id, None, 1, router, cohort_cache, None, None)
        }
        .get_match(&flags[0], None, None)
        .unwrap();

        if *result {
            assert_eq!(
                feature_flag_match,
                FeatureFlagMatch {
                    matches: true,
                    variant: None,
                    reason: FeatureFlagMatchReason::ConditionMatch,
                    condition_index: Some(0),
                    payload: None,
                }
            );
        } else {
            assert_eq!(
                feature_flag_match,
                FeatureFlagMatch {
                    matches: false,
                    variant: None,
                    reason: FeatureFlagMatchReason::OutOfRolloutBound,
                    condition_index: Some(0),
                    payload: None,
                }
            );
        }
    }
}

#[tokio::test]
async fn it_is_consistent_with_rollout_calculation_for_multivariate_flags() {
    let flags = create_flag_from_json(Some(
        json!([{
            "id": 1,
            "key": "multivariate-flag",
            "name": "Multivariate flag",
            "active": true,
            "deleted": false,
            "team_id": 1,
            "filters": {
                "groups": [
                    {
                        "properties": [],
                        "rollout_percentage": 55,
                    },
                ],
                "multivariate": {
                    "variants": [
                        {
                            "key": "first-variant",
                            "name": "First Variant",
                            "rollout_percentage": 50,
                        },
                        {
                            "key": "second-variant",
                            "name": "Second Variant",
                            "rollout_percentage": 20,
                        },
                        {
                            "key": "third-variant",
                            "name": "Third Variant",
                            "rollout_percentage": 20,
                        },
                        {
                            "key": "fourth-variant",
                            "name": "Fourth Variant",
                            "rollout_percentage": 5,
                        },
                        {
                            "key": "fifth-variant",
                            "name": "Fifth Variant",
                            "rollout_percentage": 5,
                        },
                    ],
                },
            },
        }])
        .to_string(),
    ));

    let results = vec![
        Some("second-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("fourth-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("fifth-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        Some("fifth-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("fifth-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("fourth-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("third-variant".to_string()),
        None,
        None,
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("second-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        None,
        None,
        Some("second-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("fifth-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("second-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("fifth-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("third-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("third-variant".to_string()),
        None,
        None,
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        Some("fourth-variant".to_string()),
        Some("fourth-variant".to_string()),
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("fifth-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("fifth-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("second-variant".to_string()),
        Some("fifth-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        None,
        Some("third-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("fifth-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("fourth-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("fifth-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("fourth-variant".to_string()),
        Some("fifth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("third-variant".to_string()),
        Some("fourth-variant".to_string()),
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        None,
        Some("fourth-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("fourth-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("fourth-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("fifth-variant".to_string()),
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("fourth-variant".to_string()),
        None,
        None,
        None,
        Some("fourth-variant".to_string()),
        None,
        None,
        Some("third-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        None,
        Some("fifth-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("third-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        Some("second-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("fifth-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        Some("fourth-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("fifth-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("fifth-variant".to_string()),
        None,
        None,
        Some("third-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("fifth-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        None,
        Some("second-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("fifth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("fifth-variant".to_string()),
        None,
        None,
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("second-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("fifth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("fourth-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("fifth-variant".to_string()),
        None,
        None,
        None,
        Some("second-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("second-variant".to_string()),
        None,
        None,
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        None,
        None,
        Some("first-variant".to_string()),
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("third-variant".to_string()),
        Some("second-variant".to_string()),
        Some("third-variant".to_string()),
        None,
        None,
        Some("third-variant".to_string()),
        Some("first-variant".to_string()),
        None,
        Some("first-variant".to_string()),
    ];

    for (i, result) in results.iter().enumerate().take(1000) {
        let reader = setup_pg_reader_client(None).await;
        let writer = setup_pg_writer_client(None).await;
        let cohort_cache = Arc::new(CohortCacheManager::new(reader.clone(), None, None));
        let distinct_id = format!("distinct_id_{i}");

        let feature_flag_match = {
            let router = feature_flags::database::PostgresRouter::new(
                reader.clone(),
                writer.clone(),
                reader.clone(),
                writer.clone(),
            );
            FeatureFlagMatcher::new(distinct_id, None, 1, router, cohort_cache, None, None)
        }
        .get_match(&flags[0], None, None)
        .unwrap();

        if let Some(variant) = &result {
            assert_eq!(
                feature_flag_match,
                FeatureFlagMatch {
                    matches: true,
                    variant: Some(variant.clone()),
                    reason: FeatureFlagMatchReason::ConditionMatch,
                    condition_index: Some(0),
                    payload: None,
                }
            );
        } else {
            assert_eq!(
                feature_flag_match,
                FeatureFlagMatch {
                    matches: false,
                    variant: None,
                    reason: FeatureFlagMatchReason::OutOfRolloutBound,
                    condition_index: Some(0),
                    payload: None,
                }
            );
        }
    }
}
